﻿using Basic.Api.Filters;
using Basic.Api.Models;
using OF.Component.BLL;
using OF.Component.Model;
using OF.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;

namespace Basic.Api.Controllers
{
    /// <summary>
    /// 用户权限
    /// </summary>
    public class AuthorityController: ApiController
    {
        /// <summary>
        /// 获取用户权限菜单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ActionCORS]
        [AllowAnonymous]
        public ResultData<List<AuthorityMenuModel>> GetAuthorityMenuList(int userId,string systemCode)
        {
            ResultData<List<AuthorityMenuModel>> rData = new ResultData<List<AuthorityMenuModel>>();
            if (userId < 1)
            {
                rData.status = -1;
                rData.message = "找不到此用户";
                return rData;
            }
            if (string.IsNullOrWhiteSpace(systemCode))
            {
                rData.status = -1;
                rData.message = "系统编码不能为空";
                return rData;
            }
            List<AuthorityMenuModel> menuList = new List<AuthorityMenuModel>();
            List<ChildrenMenuModel> childList = null;
            AuthorityMenuModel obj = null;
            ChildrenMenuModel childObj = null;
            IList<AuthorityMenuInfo> list = AuthorityRoleMenuRelaOperate.GetRoleMenuList(userId, systemCode);
            var plist = list.Where(a => a.ParentID == 0);
            foreach (var item in plist)
            {
                childList = new List<ChildrenMenuModel>();
                obj = new AuthorityMenuModel
                {
                    MenuCode = item.MenuCode,
                    MenuID = item.MenuID,
                    MenuName = item.MenuName,
                    MenuUrl = item.MenuUrl,
                    OrderNum = item.OrderNum                     
                };
                var Children = list.Where(a => a.ParentID == item.MenuID);
                foreach (var childItem in Children)
                {
                    childObj = new ChildrenMenuModel
                    {
                        MenuCode = childItem.MenuCode,
                        MenuID = childItem.MenuID,
                        MenuName = childItem.MenuName,
                        MenuUrl = childItem.MenuUrl,
                        OrderNum = childItem.OrderNum
                    };
                    childList.Add(childObj);
                }
                obj.Children = childList;
                menuList.Add(obj);
            }
            rData.data = menuList;
            return rData;
        }

        /// <summary>
        /// 验证用户权限 
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="menuCode"></param>
        /// <param name="systemCode"></param>
        /// <returns></returns>
        [HttpGet]
        [ActionCORS]
        [AllowAnonymous]
        public ResultData<bool> VerificationUserAuthority(int userId, string menuCode, string systemCode)
        {
            ResultData<bool> rData = new ResultData<bool>();
            if (userId < 1)
            {
                rData.status = -1;
                rData.message = "找不到此用户";
                return rData;
            }
            if (string.IsNullOrWhiteSpace(menuCode))
            {
                rData.status = -1;
                rData.message = "权限编码不能为空";
                return rData;
            }
            if (string.IsNullOrWhiteSpace(systemCode))
            {
                rData.status = -1;
                rData.message = "系统编码不能为空";
                return rData;
            }
            string key = string.Format("{0}_{1}", userId, systemCode);
            IList<AuthorityMenuInfo> list = null;
            if (CacheUtility.GetCache(userId, key) != null)
            {
                list = (IList<AuthorityMenuInfo>)CacheUtility.GetCache(userId, key);
            }
            else
            {
                list = AuthorityRoleMenuRelaOperate.GetRoleMenuList(userId, systemCode);
                CacheUtility.AddCache(list,userId, key, 10);
            }
            if (list.Where(a => a.MenuCode == menuCode).ToList().Count > 0)
            {
                rData.data = true;
            }
            else
            {
                rData.data = false;
                rData.message = "无权限";
            }
            return rData;
        }


    }
}